#include "bdd.h"

byte
td_print_base (unsigned int si)
{
  session *stmp;
  stmp = get_session (si);
  td_err = 0;
  if (!stmp)
    {
      td_err = INVALID_SESSION;
      return 0;
    }

  pouf (stmp->baseptr);
  return 1;
}


void
td_free_row (td_row * k)
{
  if (!k)
    return;
  free (k->ident);
  free (k->val);
  free (k);
}

int
td_init (char *path)
{
  int len = strlen (path);
  td_err = 0;
  chksum_crc32gentab ();
  sess_top = 0;
  add_session ();
  root_head = NULL;
  tdversion = malloc (strlen (TD_VERSION) + 1);
  strcpy (tdversion, TD_VERSION);

  bdd_path = malloc (len + 2);
  strcpy (bdd_path, path);

#ifndef WIN32
  if (bdd_path[len - 1] != '/')
    strcat (bdd_path, "/");
#else
  if (bdd_path[len - 1] != '\\')
    strcat (bdd_path, "\\");
#endif

  return 1;
}

void
td_clean (void)
{
  uint si;

  for (si = 0; si < sess_top; si++)
    if (sess_tab[si])
      free_session (si);
  free (sess_tab);

  while (root_head)
    root_head = del_base (root_head, root_head);

  free (bdd_path);
  free (tdversion);
}


unsigned int
td_new_session (unsigned int si)
{
  session *stmp;
  uint nsid;
  td_err = 0;
  stmp = get_session (si);
  if (stmp)
    {
      nsid = add_session ();
      sess_tab[nsid]->subptr = stmp->subptr;
      sess_tab[nsid]->rowptr = stmp->rowptr;
      sess_tab[nsid]->baseptr = stmp->baseptr;
      sess_tab[nsid]->rootptr = stmp->rootptr;
      return (nsid);
    }
  td_err = INVALID_SESSION;
  return 0;
}

char *
td_version (void)
{
  return tdversion;
}
